import {
  Body,
  Controller,
  Headers,
  Param,
  Post,
  UploadedFile,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { AuthGuard } from '@nestjs/passport';
import { FileInterceptor } from '@nestjs/platform-express';
import {
  ApiBody,
  ApiConsumes,
  ApiOperation,
  ApiParam,
  ApiSecurity,
  ApiTags,
} from '@nestjs/swagger';
import { ApiFile } from 'src/decorator/api-request.decorator';
import { BaseController } from 'src/logical/common/controller/base.controller';
import { BaseResultModel } from 'src/logical/common/models/BaseResult.model';
import { AccessoryModel } from '../models/accessory.model';
import { AccessoryService } from '../service/accessory.service';

@ApiSecurity('Authorization')
@ApiTags('附件相关接口')
@Controller('/apiupload')
export class AccessoryController extends BaseController {
  constructor(
    private readonly accessoryService: AccessoryService,
    private readonly jwtService: JwtService,
  ) {
    super();
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiConsumes('multipart/form-data')
  @ApiOperation({ summary: '附件上传' })
  @ApiFile()
  @UseInterceptors(FileInterceptor('file'))
  @Post('/accessoryUpload')
  async accessoryUplaod(
    @Headers('authorization') token,
    @UploadedFile() file: Express.Multer.File,
  ): Promise<BaseResultModel<AccessoryModel>> {
    const tokenInfo = this.jwtService.decode(token);

    try {
      return this.accessoryService.accessoryUpload(file, tokenInfo);
    } catch (error) {
      return this.resultData(null, 500, '上传失败');
    }
  }
}
